package it.sources.socket;

import it.sources.template.AiTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
//服务器端点
@ServerEndpoint("/socket/ai/chat/{uid}")
public class ChatSocketServer {
 public ChatSocketServer(){
 System.out.println("创建ChatSocketServer");
 }
 private static AiTemplate aiTemplate;
 @Resource
 public void setAiChatTemplate(AiTemplate aiChatTemplate){
 ChatSocketServer.aiTemplate = aiChatTemplate;
 }
 //⽤map把websocket客户端存储起来，客户端集合，key是uid
 private static ConcurrentHashMap<String, ChatSocketServer> socketClients = new ConcurrentHashMap<>();
 //会话对象，通过他来向客户端发请求
 private Session session;
 //接收uid，代表客户端⽤户
 private String uid = "";

// * 建⽴连接
// * @param session 会话
// * @param uid 连接⽤户名称

 @OnOpen
 public void onOpen(Session session, @PathParam("uid") String uid) {
 this.session = session;
 this.uid = uid;
 socketClients.put(uid, this);
 log.info("客户端{} , 建⽴连接",uid);
 }
 @OnClose
 public void onClose() {
 socketClients.remove(uid);
 log.info("客户端{} , 关闭连接",uid);
 }
 @OnMessage
 public void onMessage(String message, Session session) {
 log.info("收到消息，UID:{} 内容: {} ",uid,message);
 try {
 //向chatGPT发起请求,拿到结果
 String result =aiTemplate.chat(message) ;//chatGptTemplate.question(message);
 //给客户端返回结果
 sendMessage(result);
 }catch (Exception e){
 e.printStackTrace();
 sendMessage("AI⼩智⽣病了,请稍后重试");
 }
 }
 @OnError
 public void onError(Session session, Throwable error) {
 error.printStackTrace();
 log.error("发⽣错误，{} ",uid,error.getMessage());
 }
 public void sendMessage(String message) {
 log.error("发送消息，UID:{} ,内容：{} ",uid,message);
 try {
 this.session.getBasicRemote().sendText(message);
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 }
}